package com.easybuilder.common.api.controller;

import com.easybuilder.common.api.dto.SysRegionDTO;
import com.easybuilder.common.api.entity.SysRegion;
import com.easybuilder.common.api.service.AddressService;
import com.easybuilder.common.base.controller.BaseController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/address")
public class AddressController extends BaseController {

    @Autowired
    private AddressService addressService;

    /** 获取指定等级的城市 */
    @GetMapping("/level/{level}")
    public Result<?> getAddressByLevel(@PathVariable(name = "level", required = true) Integer level){
        List<SysRegion> list = addressService.getAddressByLevel(level);
        List<SysRegionDTO> result = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (SysRegion sysRegion : list) {
                SysRegionDTO sysRegionDTO = new SysRegionDTO();
                BeanUtils.copyProperties(sysRegion, sysRegionDTO);
                result.add(sysRegionDTO);
            }
        }
        return OK(result);
    }

    /** 获取指定城市编码下的子地区 */
    @GetMapping("/children/{parentCode}")
    public Result<?> getChildrenByParentCode(@PathVariable(name = "parentCode", required = true) Integer parentCode){
        List<SysRegion> list = addressService.getChildrenByParentCode(parentCode);
        List<SysRegionDTO> result = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (SysRegion sysRegion : list) {
                SysRegionDTO sysRegionDTO = new SysRegionDTO();
                BeanUtils.copyProperties(sysRegion, sysRegionDTO);
                result.add(sysRegionDTO);
            }
        }
        return OK(result);
    }





}
